package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.maiji.cloud.entities.login.LabelMiddleMaijiArticle;
import com.maiji.cloud.mapper.LabelMiddleMaijiArticleMapper;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.LabelMiddleMaijiArticleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.utils.UUID_MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class LabelMiddleMaijiArticleServiceImpl extends ServiceImpl<LabelMiddleMaijiArticleMapper, LabelMiddleMaijiArticle> implements LabelMiddleMaijiArticleService {

    @Override
    public BaseResDto updMaijiArticleLabel(String maijiArticleId, String labelId3, String newLabelId3, String newLabelId) {
        List<LabelMiddleMaijiArticle> lmmaList = Lists.newArrayList();
        EntityWrapper<LabelMiddleMaijiArticle> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("maiji_article_id", maijiArticleId);
        if (labelId3.equals(newLabelId)) entityWrapper.ne("label_id", labelId3);
        if (! delete(entityWrapper)) return new BaseResDto(Status.ERROR.setMessage("删除文章原标签失败"));
        if (! labelId3.equals(newLabelId)) lmmaList.add(new LabelMiddleMaijiArticle()
                .setUuId(UUID_MD5.getUUID())
                .setLabelId(newLabelId3)
                .setMaijiArticleId(maijiArticleId));
        lmmaList.add(new LabelMiddleMaijiArticle()
                .setUuId(UUID_MD5.getUUID())
                .setLabelId(newLabelId)
                .setMaijiArticleId(maijiArticleId));
        if (! insertBatch(lmmaList)) return new BaseResDto(Status.ERROR.setMessage("保存文章新标签失败"));
        return new BaseResDto(Status.SUCCESS);
    }

}
